package com.sxg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxg.constant.CacheConstants;
import com.sxg.convert.SxgGoodsCollectionConvert;
import com.sxg.dao.SxgGoodsCollectionDao;
import com.sxg.entity.SxgGoodsCollectionEntity;
import com.sxg.entity.SxgGoodsInfoEntity;
import com.sxg.query.SxgGoodsCollectionQuery;
import com.sxg.redis.RedisCache;
import com.sxg.service.SxgGoodsCollectionService;
import com.sxg.service.SxgGoodsInfoService;
import com.sxg.utils.SecurityUtils;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgGoodsCollectionVO;
import com.sxg.vo.req.GoodsCollectionReqVo;
import com.sxg.vo.resp.CollectionRespVo;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品收藏
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@Service
@AllArgsConstructor
public class SxgGoodsCollectionServiceImpl extends ServiceImpl<SxgGoodsCollectionDao, SxgGoodsCollectionEntity> implements SxgGoodsCollectionService {

    @Autowired
    private SxgGoodsInfoService sxgGoodsInfoService;

    @Resource
    private SxgGoodsCollectionDao sxgGoodsCollectionDao;

    @Resource
    private RedisCache redisCache;

    @Override
    public PageResult<CollectionRespVo> page(SxgGoodsCollectionQuery query) {
        Integer count = sxgGoodsCollectionDao.getByUerIdListCount(SecurityUtils.getUserId());
        List<CollectionRespVo> byUerIdList = sxgGoodsCollectionDao.getByUerIdList(SecurityUtils.getUserId(), query.getTime(), query.getLimit(), (query.getPage() - 1) * query.getLimit());
        return new PageResult<>(byUerIdList, count);
    }


    @Override
    public void save(SxgGoodsCollectionVO vo) {
        SxgGoodsCollectionEntity entity = SxgGoodsCollectionConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SxgGoodsCollectionVO vo) {
        SxgGoodsCollectionEntity entity = SxgGoodsCollectionConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delete(List<Long> idList) {

        if (idList.isEmpty()) {
            return AjaxResult.error("请选择要删除的商品");
        }
        //查询用户是否有该商品收藏
        List<SxgGoodsCollectionEntity> collectionEntities = baseMapper.selectList(new QueryWrapper<SxgGoodsCollectionEntity>().select("id").eq("user_id", SecurityUtils.getUserId()).in("id", idList));
        if (collectionEntities.size() < idList.size()) {
            return AjaxResult.error("没有该商品收藏");
        }
        //删除用户下的该类商品
        baseMapper.delete(new QueryWrapper<SxgGoodsCollectionEntity>().eq("user_id", SecurityUtils.getUserId()).in("id", idList));

        return AjaxResult.success();
    }

    @Override
    public AjaxResult saveByUserId(GoodsCollectionReqVo vo) {

        // 判断商品是否存在
        SxgGoodsInfoEntity goodsInfo = sxgGoodsInfoService.getOne(new QueryWrapper<SxgGoodsInfoEntity>().select("id").eq("id", vo.getGoodsId()));
        if (goodsInfo == null) {
            return AjaxResult.error("商品不存在");
        }

        if ("1".equals(vo.getStatus())) {
            // 判断用户是否已经收藏该商品
            SxgGoodsCollectionEntity collection = baseMapper.selectOne(new QueryWrapper<SxgGoodsCollectionEntity>().eq("user_id", SecurityUtils.getUserId()).eq("goods_id", vo.getGoodsId()));
            if (collection != null) {
                return AjaxResult.error("您已收藏该商品");
            }
            // 收藏商品
            SxgGoodsCollectionEntity sxgGoodsCollectionEntity = new SxgGoodsCollectionEntity();
            sxgGoodsCollectionEntity.setGoodsId(vo.getGoodsId());
            sxgGoodsCollectionEntity.setUserId(SecurityUtils.getUserId());
            save(sxgGoodsCollectionEntity);
        } else {
            // 取消收藏商品
            getBaseMapper().delete(new QueryWrapper<SxgGoodsCollectionEntity>().eq("user_id", SecurityUtils.getUserId()).eq("goods_id", vo.getGoodsId()));
        }

        return AjaxResult.success();
    }

}